/**
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>Company: ---Technology Co. Ltd</p>
 * <p>2014-4-30</p>
 * @author Jan.Zhang
 */
package com.xianl.busiplat.service.impl;

import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.collect.Sets;
import com.xianl.busiplat.dao.BaseDao;
import com.xianl.busiplat.dao.RoleDao;
import com.xianl.busiplat.po.Role;
import com.xianl.busiplat.po.RolePermission;
import com.xianl.busiplat.service.RolePermissionService;
import com.xianl.busiplat.service.RoleService;
import com.xianl.busiplat.shiro.ShiroDbRealm;


/** 
 * desc:
 * <p>author: Jan.Zhang </p>
 * date: 2014-4-30
 */
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {

	private RoleDao mapper;
	
	private RolePermissionService rolePermissionService;
	
	@Autowired
	private ShiroDbRealm shiroDbRealm;
	
	
	/* 
	 * desc:
	 * @see com.umpay.sc.plat.core.service.impl.BaseServiceImpl#save(java.lang.Object)
	 */
	@Override
	public void save(Role role) {
		Set<String> psSet = Sets.newHashSet();
		if(role.getPermissions() != null){
			psSet.addAll(role.getPermissions());
		}
		mapper.insert(role);
		RolePermission permission = new RolePermission();
		permission.setRoleId(role.getId());
		for(String s : psSet){
			permission.setPermission(s);
			rolePermissionService.save(permission);
		}
		shiroDbRealm.clearAllCachedAuthorizationInfo();
	}
	
	/* 
	 * desc:
	 * @see com.umpay.sc.plat.core.service.RoleService#get(int)
	 */
	@Override
	public Role get(Integer id) {
		Role role = new Role();
		role.setId(id);
		return mapper.selectOne(role);
	}

	/* 
	 * desc:
	 * @see com.umpay.sc.plat.core.service.RoleService#delete(int)
	 */
	@Override
	public void delete(Integer id) {
		// 需要先删除角色权限
		rolePermissionService.deletePermissionsByRoleId(id);
		Role role = new Role();
		role.setId(id);
		mapper.delete(role);
		shiroDbRealm.clearAllCachedAuthorizationInfo();
		
	}
	
	@Override
	public void update(Role role){
		List<String> oldpsList = rolePermissionService.findPermissions(role.getId());
		Set<String> oldpsSet = Sets.newHashSet();
		if(oldpsList != null){
			oldpsSet.addAll(oldpsList);
		}
		Set<String> newpsList = Sets.newHashSet(role.getPermissions());
		RolePermission permission = new RolePermission();
		permission.setRoleId(role.getId());
		// 插入新增的权限
		for(String s : newpsList){
			if(oldpsSet.contains(s)){// 新旧权限重复部分
				oldpsSet.remove(s);
			}else{
				permission.setPermission(s);
				rolePermissionService.save(permission);
			}
		}
		// 删除权限
		for(String s : oldpsSet){
			permission.setPermission(s);
			rolePermissionService.delete(permission);
		}
		mapper.update(role);
		shiroDbRealm.clearAllCachedAuthorizationInfo();
	}
	
	/**
	 * @param rolePermissionService the rolePermissionService to set
	 */
	public void setRolePermissionService(RolePermissionService rolePermissionService) {
		this.rolePermissionService = rolePermissionService;
	}

	@Override
	public void setMapper(BaseDao<Role> mapper) {
		super.setMapper(mapper);
		this.mapper = (RoleDao) mapper;
	}
}
